<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ～ ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\registration\excel;

use app\common\model\FollowTrial;
use catcher\library\excel\ExcelContract;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

class TrialExport implements ExcelContract
{

    /**
     * 设置头部
     *
     * @time 2020年09月08日
     * @return string[]
     */
    public function headers(): array
    {
        // TODO: Implement headers() method.
        $req = request()->param();
        $field = [];
        foreach ($req['title'] as $key => $val) {
            $field[] = $val['label'];
        }
        return $field;
    }

    /**
     * 处理数据
     *
     * @time 2020年09月08日
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @return array
     */
    public function sheets(): array
    {
        // TODO: Implement sheets() method.
        $req = request()->param();

        $where = [];
        if (!empty($req['patientId'])) {
            $where[] = ['patientId', '=', $req['patientId']];
        }

        $field = [];
        foreach ($req['title'] as $key => $val) {
            if ($val['prop'] == 'resultName') {
                $field[] = 'result';
            }else{
                $field[] = $val['prop'];
            }

        }
        $fieldStr = implode(',', $field);
        $data = (new FollowTrial())->getDatas($where, $fieldStr)->toArray();

        $ret = [];
        foreach ($field as $item) {
            foreach ($data as $key => $val) {
                //处理身份证号
                if ($item == 'idno' && !empty($val['idno'])) {
                    $ret[$key][$item] = $val['idno'].' ';
                } elseif ($item == 'result' && !empty($val['result'])) {
                    $names = array_column($val['result'], 'name');
                    $ret[$key][$item] = implode(',', $names);
                }else{
                    $ret[$key][$item] = $val[$item];
                }
            }
        }
        return $ret;
    }

    /**
     * 设置开始行
     *
     * @time 2020年09月08日
     * @return int
     */
    public function setRow(): int
    {
        return 2;
    }

    /**
     * 设置标题
     *
     * @time 2020年09月08日
     * @return array
     */
    public function setTitle(): array
    {
        $req = request()->param();
        $count = count($req['title']);
        $title = config('dictionary.trial_follow_title');
        return [
            'A1:'.$title[$count], '导出随访记录', Alignment::HORIZONTAL_CENTER
        ];
    }
}